/**********************************************************************************
 * Copyright (c) 2012 - 2012,  parallel, All right reserved
 * Title: com.adc.parallel.daemon  ReceiveDaemonThread.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-11-21 下午10:15:15 
 *********************************************************************************/


package com.adc.parallel.daemon;

import java.util.List;

import com.adc.parallel.constants.ReceiveList;
import com.adc.parallel.constants.SysConstants;
import com.adc.parallel.constants.ThreadConstant;
import com.adc.parallel.constants.WaitList;
import com.adc.parallel.dto.BaseTaskDto;
import com.adc.quicktools.StringUtil;


/**
 * 
 * Description: used to deal with receive task dto List timely
 * @author shen  shenqiao2012@163.com
 * @param <E>
 * @param <T>
 */
public class ReceiveDaemonThread<E extends BaseTaskDto<T>, T> extends BaseTimerThread {

	private String identity;
	private List<E> receiveList;
	/**
	 * 
	* @param identity
	* @param receiveList
	 */
	public ReceiveDaemonThread(String identity,List<E> receiveList) {
		super(ThreadConstant.getReceiveTime(), ThreadConstant.getReceiveTime());
		this.setName(SysConstants.getDaemonThreadName(identity));
		this.identity=identity;
		this.receiveList=receiveList;
	}

	/**
	 * Description:  
	 * @see com.adc.parallel.daemon.BaseTimerThread#operaPerCycle() 
	 */
	@Override
	public int operaPerCycle() {
		// TODO Auto-generated method stub
//		System.out.println("\t"+this.getName()+"\t is on the working!");
		int rs=ThreadConstant.EXEC_FAIL;
		if (receiveList!=null && receiveList.size()>0 && StringUtil.checkStringValue(identity)) {
			if (SysConstants.ListType_RE.equals(identity)) {
				rs=pushTaskDtoToWaitList(receiveList);
			}
		}
		return rs;
	}

	/**
	 * Description:  
	 * @see com.adc.parallel.daemon.BaseTimerThread#operaOnTimeOut() 
	 */
	@Override
	public int operaOnTimeOut() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	/**
	 * 
	 * Description: push task dto in receive list to wait list
	 * author:  shen
	 * @param receiveList
	 * @return int
	 */
	private int pushTaskDtoToWaitList(final List<E> receiveList){
		for (int i = 0; i < receiveList.size(); i++) {
			WaitList.addTaskDto(receiveList.get(i));
		}
		ReceiveList.removeAllDto(receiveList);
		return ThreadConstant.EXEC_SUCCESS;
	}

}
